import time

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

# 用户
username = '15169445086'
password = '123@ABCabc'
# 项目经理
project_manager = '张继秋'
# 项目名称
project_name = '基于Spark的机器学习建模框架设计与开发'
# 项目性质 2 研发项目 1 普通项目
project_nature = 2

driver = webdriver.Chrome(executable_path=".\chromedriver.exe")
driver.get("http://221.0.92.204:9889/Login/Index")
usernameInput = driver.find_element_by_id("mk_username")
passwordInput = driver.find_element_by_id("mk_password")
usernameInput.send_keys(username)
passwordInput.send_keys(password)
# driver.execute_script("return arguments[0].value = '%s';" % username, usernameInput)
# driver.execute_script("return arguments[0].value = '%s';" % password, passwordInput)
driver.find_element_by_id("mk_login_btn").click()
time.sleep(10)

filloutElement = driver.find_element_by_xpath("//*[@title='个人']")
ActionChains(driver).move_to_element(filloutElement).perform()
filloutElement.find_element_by_xpath("//*[@title='工时填报']").click()
driver.switch_to.frame("mk_iframe_9d6d2999-ae1a-48c0-83ce-798aa763d8d0")
mk_add = driver.find_element_by_id("mk_add")
time.sleep(1)

ActionChains(driver).move_to_element(mk_add).perform()
mk_add.click()
time.sleep(1)
driver.switch_to.default_content()
driver.switch_to.frame("mk_iframe_GSTBform-add")
workingTitle = driver.find_element_by_xpath("//*[@id='F_WorkingTitle']")
workingTitle.send_keys("工时填报")
manager1 = driver.find_element_by_id("F_ProjectManager1")
manager1.click()

time.sleep(1)
# 切换弹窗iframe
driver.switch_to.default_content()
driver.switch_to.frame("layui-layer-iframe1")
selectManager = driver.find_element_by_xpath("//*[@id='userId']/div")
selectManager.click()
selectManager = driver.find_element_by_xpath(
    "//*[@id='minke_select_option_userId']/*[@class='mk-select-option-search']/input")
selectManager.send_keys(project_manager)
selectManager.send_keys(Keys.ENTER)
selectedManger = driver.find_element_by_xpath("//*[@id='minke_select_option_contentuserId']/li[2]")
selectedManger.click()
driver.switch_to.default_content()
driver.find_element_by_class_name("layui-layer-btn0").click()

# 切换到工时填报页面
driver.switch_to.frame("mk_iframe_GSTBform-add")
# 点击添加项目按钮
driver.find_element_by_xpath("//*[@id='jfgrid_toolbar_Zgy_XM_WorkingHoursAllowedDetail']/span[1]/i").click()
time.sleep(1)
projectName = driver.find_element_by_xpath("//*[@id='jfgrid_right_Zgy_XM_WorkingHoursAllowedDetail']/div[5]")
projectName.click()
projectSelect = driver.find_element_by_xpath(
    "//*[@id='jfgrid_edit_Zgy_XM_WorkingHoursAllowedDetail_F_ProjectName']/div")
projectSelect.click()
time.sleep(0.5)

# 选择项目
projectUl = driver.find_element_by_xpath(
    "//*[@id='minke_select_option_jfgrid_edit_Zgy_XM_WorkingHoursAllowedDetail_F_ProjectName']/*[@class='mk-select-option-search']/input")
projectUl.send_keys(project_name)
projectUl.send_keys(Keys.ENTER)

projectUlNode = projectUl.find_elements_by_xpath("//*[@class='mk-tree-node-ct']/*[@class='mk-tree-node']/div")
projectUlNode[0].click()

# 选择项目性质
projectNatureF = driver.find_element_by_xpath("//*[@id='jfgrid_right_Zgy_XM_WorkingHoursAllowedDetail']/div[6]")
projectNatureF.click()
projectNature = driver.find_element_by_xpath(
    "//*[@id='jfgrid_edit_Zgy_XM_WorkingHoursAllowedDetail_F_ProjectNature']/div")
projectNature.click()
time.sleep(0.5)
projectNatureUl = driver.find_element_by_xpath(
    "//*[@id='minke_select_option_contentjfgrid_edit_Zgy_XM_WorkingHoursAllowedDetail_F_ProjectNature']/li["+str(project_nature+1)+"]")
projectNatureUl.click()

# 工时
projectTimeF = driver.find_element_by_xpath("//*[@id='jfgrid_right_Zgy_XM_WorkingHoursAllowedDetail']/div[7]")
projectTimeF.click()
projectTime = driver.find_element_by_xpath(
    "//*[@id='jfgrid_edit_Zgy_XM_WorkingHoursAllowedDetail_F_WorkingHours']")
projectTime.send_keys(7)


saveDocument = driver.find_element_by_xpath("//*[@class='top-btn-tool']")
saveDocument.click()

